package xyz.yuzegod.megawalls.classes.normal.hunter;

import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.nms.*;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.EntityTypes;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.*;

public class SecondSkill extends Skill
{
    private final Map<String, Class<? extends CustomEntity>> classMap;
    
    public SecondSkill(final Classes classes) {
        super("动物伙伴", classes);
        (this.classMap = new HashMap<String, Class<? extends CustomEntity>>()).put("皮皮猪", CustomPig.class);
        this.classMap.put("蜘蛛", CustomSpider.class);
        this.classMap.put("爆炸羊", BoomSheep.class);
        this.classMap.put("狼", ShamanWolf.class);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.1;
            }
            case 2: {
                return 0.15;
            }
            case 3: {
                return 0.2;
            }
            default: {
                return 0.1;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7当你被攻击,你将有§a" + StringUtils.percent(this.getAttribute(level)) + "§7的");
            lore.add("   §7几率召唤一只随机宠物。");
            lore.add("   §7它可能是:皮皮猪、蜘蛛");
            lore.add("   §7爆炸羊或狼。");
            lore.add(" ");
            lore.add("§7冷却时间:§a4秒");
            return lore;
        }
        lore.add(" §8▪ §7当你被攻击,你将有§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7的");
        lore.add("   §7几率召唤一只随机宠物。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill2Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill2Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        if (Hunter.skill2Cooldown.getOrDefault(gamePlayer, 0) > 0) {
            return false;
        }
        try {
            final List<Map.Entry<String, Class<? extends CustomEntity>>> list = new ArrayList<Map.Entry<String, Class<? extends CustomEntity>>>(this.classMap.entrySet());
            final Map.Entry<String, Class<? extends CustomEntity>> entry = list.get(new Random().nextInt(list.size() - 1));
            final CustomEntity customEntity = (CustomEntity)entry.getValue().getConstructor(World.class).newInstance(((CraftWorld)Bukkit.getWorld("world")).getHandle());
            customEntity.setGamePlayer(gamePlayer);
            customEntity.getBukkitEntity().setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
            EntityTypes.spawnEntity((Entity)customEntity, gamePlayer.getPlayer().getLocation());
            gamePlayer.sendMessage("§a你的动物伙伴技能召唤了一只" + entry.getKey() + "！");
        }
        catch (Exception ex) {}
        Hunter.skill2Cooldown.put(gamePlayer, 4);
        return true;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.getClasses().getNameColor() + "§l" + this.getName() + " " + ((Hunter.skill2Cooldown.getOrDefault(gamePlayer, 0) == 0) ? "§a§l✓" : ("§c§l" + Hunter.skill2Cooldown.get(gamePlayer) + "秒"));
    }
}
